package com.guo.core.framework.template;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ServiceManager {

    private static final Map<String, Map<String,ExecuteService<?,?>>> serviceMap = new HashMap<>();

    public static void registerService(BizService service, ServiceType serviceType, ExecuteService<?,?> serviceImpl) {
        log.info("服务[{}.{}]注册", service.getCode(),serviceType.getCode());
        if (!serviceMap.containsKey(service.getCode())) {
            Map<String,ExecuteService<?,?>> serviceTypeMap = new HashMap<>();
            serviceTypeMap.putIfAbsent(serviceType.getCode(), serviceImpl);
            serviceMap.putIfAbsent(service.getCode(), serviceTypeMap);
        }

        serviceMap.get(service.getCode()).putIfAbsent(serviceType.getCode(), serviceImpl);
    }

    public static ExecuteService getService(BizService service, ServiceType serviceType) {
        Map<String,ExecuteService<?,?>> serviceTypeMap = serviceMap.get(service.getCode());
        if (serviceTypeMap == null) {
            log.info("服务[{}]不存在", service.getCode());
            return null;
        }
        ExecuteService executeService = serviceTypeMap.get(serviceType.getCode());
        if (executeService == null) {
            log.info("服务[{}.{}]不存在", service.getCode(),serviceType.getCode());
            return null;
        }

        return executeService;
    }

}
